package com.yykt.app.part7;

import ch.qos.logback.classic.spi.EventArgUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.*;

@Slf4j
public class Demo {


    private double mockXgBoost(int a, int b) {
        return a / b;
    }
    private double getRate(int a, int b) {
        double rate = 0;
        try {
            rate = mockXgBoost(a, b);
        } catch (Exception e) {
            log.info("a: {}, b: {}", a, b, e);
            e.printStackTrace();
        }
        return rate;
    }

    @Test
    void test() {
        System.out.println("process begin!");
        int a = 20;
        int b = 0;
        double rate = getRate(a, b);
        System.out.println(rate);
        System.out.println("process end!");
    }

    @Test
    void testLoadFile() throws IOException {
        String file = "date/demo.txt";
        String path = Demo.class.getClassLoader().getResource(file).getPath();

        BufferedReader reader = new BufferedReader(new FileReader(path));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

    }

    @Test
    void testReaderWriter() {
        String file = "date/demo.txt";
        String path = Demo.class.getClassLoader().getResource(file).getPath();
        String outputfile = "res.txt";

        try (
                BufferedReader reader = new BufferedReader(new FileReader(path));
                BufferedWriter writer = new BufferedWriter(new FileWriter(outputfile));
        ) {
            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

}
